﻿'
' Boogaloo® - http://www.mindfly.com
' Copyright (c) 2008
' by Mindfly, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
' INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
' SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
' WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
' THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


Imports Boogaloo.Core.UI
Imports Boogaloo.Core.BLL
Imports Boogaloo.Admin.UI.Controls

Namespace UI.UserControls


    Partial Public Class Site_ContentPlaceHolderManager
        Inherits ContentPlaceHolderManagerBase

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            div_designer.Visible = False
            Dim labelString As String = ""
            Select Case usage
                Case cphmanager_usage.embedded_controls
                    obj_node = Node.GetNode(Groove.admin_root.embedded_control_node_id)
                    labelString = "Place a user control in this host's embedded controls"
                Case cphmanager_usage.layout
                    obj_node = Groove.admin_current_node
                    labelString = "Place a user control in this layout"
                Case cphmanager_usage.page                    
                    labelString = "Add a User Control"
                    obj_node = Groove.admin_current_node
                    If obj_node.managed_on_publish Then publish_id = Groove.publish_admin_working.publish_id
                    div_designer.Visible = True
            End Select
            litOpen.Text = "<span class='buttonWrap'><button type='button' class='button add reveal'>+</button><span>" & labelString & "</span></span>"

            If obj_node IsNot Nothing AndAlso obj_node.node_type_id = NodeType.WebPage Then

                Dim cphs As New ArrayList
                cphs.AddRange(contentplaceholder_list)
                cphs.Add(orphaned_control_title)
                rpt_cph.DataSource = cphs
                rpt_cph.DataBind()

                If Not Page.IsPostBack Then BindControl()

            Else
                Me.Visible = False
            End If

        End Sub


        Private Sub BindControl()

            If div_designer.Visible Then



                lhddl_masterpage.Visible = True
                With lhddl_masterpage.dropdownlist
                    .DataSource = Groove.admin_root.master_templates()
                    .DataTextField = "name"
                    .DataValueField = "master_template_id"
                    .DataBind()
                    .SelectedValue = publish_mastertemplate.master_template_id
                End With
                lhddl_masterpage.CommandArgument = "masterpage"
                lhddl_masterpage.label = String.Format("{0}", publish_mastertemplate.name)
                If publish_id > 0 Then
                    lhddl_masterpage.Enabled = False
                    lhddl_masterpage.ToolTip = "Disabled until working version of the page is published"
                End If


                With lhddl_theme.dropdownlist
                    .DataSource = Groove.admin_root.themes
                    .DataTextField = "name"
                    .DataValueField = "theme_id"
                    .DataBind()
                    .Items.Insert(0, New ListItem("None", 0))
                    .SelectedValue = obj_node.theme_id.ToString
                End With
                lhddl_theme.CommandArgument = "theme"
                If obj_node.theme IsNot Nothing Then
                    lhddl_theme.label = String.Format("{0}", obj_node.theme.name)
                Else
                    lhddl_theme.label = "This page does not use ASP.NET themes"
                End If

            End If

            ddl_control.DataSource = RegisteredControl.GetControls(Groove.admin_root.root_id)
            ddl_control.DataTextField = "name"
            ddl_control.DataValueField = "control_id"
            ddl_control.DataBind()
            ddl_control.Items.Insert(0, New ListItem("Please select", 0))

            ddl_cph.DataSource = contentplaceholder_list
            ddl_cph.DataBind()


        End Sub


        Protected Sub rpt_cph_ItemDataBind(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles rpt_cph.ItemDataBound

            Dim cph As String = CType(e.Item.DataItem, String)
            If Not String.IsNullOrEmpty(cph) Then

                Dim lit_cph As Literal = CType(e.Item.FindControl("lit_cph"), Literal)
                lit_cph.Text = cph

                Dim cphitem As Site_ContentPlaceHolderManagerItem = Me.LoadControl(String.Format("{0}Site_ContentPlaceHolderManagerItem.ascx", Groove.path_admin_controls))
                cphitem.ID = "cphitem_" & e.Item.ItemIndex.ToString
                cphitem.orphaned_control_title = Me.orphaned_control_title
                cphitem.obj_node = Me.obj_node
                cphitem.usage = Me.usage
                cphitem.cph = cph

                Dim ph As PlaceHolder = CType(e.Item.FindControl("ph"), PlaceHolder)
                ph.Controls.Add(cphitem)

            End If

        End Sub


        Protected Sub lb_add_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lb_add.Click

            If Page.IsValid Then

                Dim publish_id As Integer = 0
                If obj_node.managed_on_publish Then publish_id = Groove.publish_admin_working.publish_id

                Dim node_control_id As Integer = NodeControl.InsertNodeControl( _
                    obj_node.node_id, _
                    CType(ddl_control.SelectedValue, Integer), _
                    txt_name.Text, _
                    ddl_cph.SelectedValue, 0, publish_id)
            End If

            Groove.RefreshPage()

        End Sub


        Protected Sub lhddl_Click(ByVal sender As Object, ByVal e As EventArgs) Handles lhddl_masterpage.Click, lhddl_theme.Click

            Dim lhddl As LabelHiddenDropDownBox = CType(sender, LabelHiddenDropDownBox)
            Dim id As Integer = CType(lhddl.dropdownlist.SelectedValue, Integer)
            Dim refresh As Boolean = False
            Dim publish_id As Integer = 0
            If obj_node.managed_on_publish Then publish_id = Groove.publish_admin_working.publish_id
            If lhddl.CommandArgument = "masterpage" Then
                If publish_id = 0 Then
                    obj_node.master_template_id = id
                    refresh = True
                Else
                    Dim pts As List(Of PublishTask) = PublishTask.GetPublicTasksByTaskName(publish_id, "setmasterpage")
                    If pts.Count > 0 Then
                        pts(0).parameter_list = id
                        pts(0).Update()
                    Else
                        PublishTask.InsertPublishTask(publish_id, "setmasterpage", id)
                    End If
                End If
            ElseIf lhddl.CommandArgument = "theme" Then
                obj_node.theme_id = id
            End If
                obj_node.Update()

                If refresh Then
                    Groove.RefreshPage()
                Else
                    BindControl()
                End If

        End Sub

    End Class

End Namespace